﻿using Huirui.Cavan.Domain.MainBoundedContext.PurchaseModule;
using Huirui.Cavan.Domain.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.Seedwork;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Huirui.Cavan.Core.Extension;
using Huirui.Cavan.Domain.MainBoundedContext.SalesModule;

namespace Huirui.Cavan.Domain.MainBoundedContext.FinanceModule
{
    public class Accountant
    {
        private Employee _employee;

        public Accountant(Employee employee)
        {
            this._employee = employee;
        }

        public BoolGenericResult<Pay> Pay(string id, DateTime payDate, Account fromAccount, string accountNumber, decimal amount, decimal discount, string currency,
            decimal currencyRate, string remark, Employee payEmployee, Supplier supplier, IList<PayDetail> payDetails)
        {
            BoolGenericResult<Pay> result = new BoolGenericResult<Pay>();

            if (payDetails.IsNull() || !payDetails.Any(pd => pd.Amount > 0 || pd.Amount < 0))
            {
                result.IsSuccess = false;
                result.Messages.Add("没有有效的支付明细");
                return result;
            }

            Pay pay = PurchaseModuleFactory.CreatePay(id, payDate, fromAccount, accountNumber, amount, discount, currency, currencyRate,
                remark, DateTime.Now, DateTime.Now, this._employee.UserAccount, this._employee.Corporation, payEmployee, supplier);

            foreach (PayDetail pd in payDetails)
            {
                pd.UnPay.LeftAmount -= pd.Amount;
                PayDetail newpd = PurchaseModuleFactory.CreatePayDetail(pay, pd.UnPay, pd.Amount, pd.CertificateDate, DateTime.Now, DateTime.Now, this._employee.UserAccount.Id);
                pay.PayDetails.Add(newpd);
            }

            if (fromAccount.IsNotNull())
            {
                fromAccount.LeftAmount -= amount;
            }

            result.Object = pay;

            return result;
        }

        public BoolGenericResult<Pay> RePay(Pay oldPay, IList<PayDetail> payDetails)
        {
            BoolGenericResult<Pay> result = new BoolGenericResult<Pay>();

            if (payDetails.IsNull() || !payDetails.Any(pd => pd.Amount > 0 || pd.Amount < 0))
            {
                result.IsSuccess = false;
                result.Messages.Add("没有有效的支付明细");
                return result;
            }

            foreach (PayDetail pd in payDetails)
            {
                pd.UnPay.LeftAmount -= pd.Amount;
                PayDetail newpd = PurchaseModuleFactory.CreatePayDetail(oldPay, pd.UnPay, pd.Amount, pd.CertificateDate, DateTime.Now, DateTime.Now, this._employee.UserAccount.Id);
                oldPay.PayDetails.Add(newpd);
            }
            result.Object = oldPay;

            return result;
        }

        public BoolGenericResult<Receipt> Receive(string id, DateTime receiptDate, Account toAccount, string accountNumber, decimal amount, decimal discount, string currency, 
            decimal currencyRate, Employee receiptEmployee, string remark, Customer customer, IList<ReceiptDetail> receiptDetails)
        {
            BoolGenericResult<Receipt> result = new BoolGenericResult<Receipt>();

            Receipt receipt = SalesModuleFactory.CreateReceipt(id, receiptDate, toAccount, accountNumber, amount, discount, currency, currencyRate, receiptEmployee, remark,
                DateTime.Now, DateTime.Now, this._employee.UserAccount.Id, this._employee.Corporation, customer);

            foreach (ReceiptDetail rd in receiptDetails)
            {
                rd.Receivables.LeftAmount -= rd.Amount;
                ReceiptDetail receiptDetail = SalesModuleFactory.CreateReceiptDetail(rd.Amount, rd.CertificateDate, DateTime.Now, DateTime.Now, this._employee.UserAccount.Id, receipt, rd.Receivables);
                receipt.ReceiptDetails.Add(receiptDetail);
            }

            if (toAccount.IsNotNull())
            {
                toAccount.LeftAmount += amount;
            }

            result.Object = receipt;

            return result;
        }
    }
}
